package com.saudidentalapp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.saudiapp.lazylist.ImageLoader;
import com.saudiapp.webservice.Constants;
import com.saudiapp.webservice.Responce;
import com.saudiapp.webservice.Utilities;
import com.saudiapp.webservice.Utilities.RatingDialogListener;
import com.saudiapp.webservice.WebServiceManager;
import com.saudiapp.webservice.WebServiceManager.WebServiceListener;
import com.saudidentalapp.datamodels.Speaker;

public class AboutSpeakers extends Activity implements WebServiceListener ,OnClickListener
{
	TextView txtName,txtDesignation,txtDescription;
	ImageView imgImage;
	TextView txtwebsite,txtphone;	
	ImageView imgFacebook,imgTwitter,imgLinkedn,imgPdf,imgWeb,imgContact;
	
	Utilities utilities;
	
	ImageView imgEmail,imgRating,ximgComment;
	
	Speaker speaker;
	AboutSpeakers activity=this;
	
	
	View vwSeperator;
	 @Override
	protected void onCreate(Bundle savedInstanceState) 
	 {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about_speakers);			
		txtName=(TextView)findViewById(R.id.xtxtName);
		txtDesignation=(TextView)findViewById(R.id.xtxtDesignation);
		txtDescription=(TextView)findViewById(R.id.xtxtDescription);		
		imgImage=(ImageView)findViewById(R.id.ximgspeakerImage);
		
		setView();
	}
	 
	private void setView()
	{
		speaker=(Speaker) getIntent().getSerializableExtra("MyClass");
		
		txtName.setText(speaker.getName());
		txtDesignation.setText(speaker.getLocation());
		txtDescription.setText(speaker.getDescription());
		
		ImageLoader loader= new ImageLoader(activity);
		loader.DisplayImage(speaker.getImage(),imgImage);	
		
		txtwebsite=(TextView)findViewById(R.id.xtxtweb);		
		txtphone=(TextView)findViewById(R.id.xtxtcontact);		
		
		imgWeb=(ImageView)findViewById(R.id.ximgweb);
		imgContact=(ImageView)findViewById(R.id.ximgcall);
		imgFacebook=(ImageView)findViewById(R.id.ximg_fb);
		imgTwitter=(ImageView)findViewById(R.id.ximg_tw);
		imgLinkedn=(ImageView)findViewById(R.id.ximg_in);		
		imgPdf=(ImageView)findViewById(R.id.ximg_pdf);		
		vwSeperator=(View)findViewById(R.id.view2);	
		if(speaker.getWebsite()!=null)
		{
			if(!speaker.getWebsite().equalsIgnoreCase("#") && !speaker.getWebsite().equalsIgnoreCase(""))
			{	
				imgWeb.setTag(""+speaker.getWebsite());
				imgWeb.setVisibility(View.VISIBLE);
				txtwebsite.setText(speaker.getWebsite());
				vwSeperator.setVisibility(View.VISIBLE);
			}
			else
			{
				imgWeb.setVisibility(View.GONE);
				txtwebsite.setText(""+Constants.DATA_NOT_AVAILABELE);
			}
			
		}
		
		
		if(speaker.getContactNo()!=null)
		{
			if(!speaker.getContactNo().equalsIgnoreCase("#")&& !speaker.getContactNo().equalsIgnoreCase(""))
			{	
				imgContact.setVisibility(View.VISIBLE);
				imgContact.setTag(""+speaker.getContactNo());
				txtphone.setText(speaker.getContactNo());
				vwSeperator.setVisibility(View.VISIBLE);
			}
			else
			{
				imgContact.setVisibility(View.GONE);
				txtphone.setText(""+Constants.DATA_NOT_AVAILABELE);
			}
		}
		
		
		
		
		if(speaker.getFacebookURL()!=null)
		{
			if(!speaker.getFacebookURL().equalsIgnoreCase("#")&& !speaker.getFacebookURL().equalsIgnoreCase(""))
			{	
				imgFacebook.setVisibility(View.VISIBLE);
				imgFacebook.setTag(""+speaker.getFacebookURL());	
				vwSeperator.setVisibility(View.VISIBLE);
			}
			else
			{
				imgFacebook.setVisibility(View.GONE);
				
			}
		}
		
		
		if(speaker.getTwitterURL()!=null)
		{
			if(!speaker.getTwitterURL().equalsIgnoreCase("#")&& !speaker.getTwitterURL().equalsIgnoreCase(""))
			{	
				imgTwitter.setVisibility(View.VISIBLE);
				imgTwitter.setTag(""+speaker.getTwitterURL());	
				vwSeperator.setVisibility(View.VISIBLE);
			}
			else
			{
				imgTwitter.setVisibility(View.GONE);
				
			}
		}
		
		
		if(speaker.getLinkedInURL()!=null)
		{
			if(!speaker.getLinkedInURL().equalsIgnoreCase("#")&& !speaker.getLinkedInURL().equalsIgnoreCase(""))
			{	
				imgLinkedn.setVisibility(View.VISIBLE);
				imgLinkedn.setTag(""+speaker.getLinkedInURL());	
				vwSeperator.setVisibility(View.VISIBLE);
			}
			else
			{
				imgLinkedn.setVisibility(View.GONE);
				
			}
		}
		
		if(speaker.getPdfFile()!=null)
		{
			if(!speaker.getPdfFile().equalsIgnoreCase("#")&& !speaker.getPdfFile().equalsIgnoreCase(""))
			{	
				imgPdf.setVisibility(View.VISIBLE);
				imgPdf.setTag(""+speaker.getPdfFile());	
				vwSeperator.setVisibility(View.VISIBLE);
			}
			else
			{
				imgPdf.setVisibility(View.GONE);
				
			}
		}
		
		
		
	
		//imgFacebook.setTag(""+speaker.getFacebookURL());
		//imgTwitter.setTag(""+speaker.getTwitterURL());
		//imgLinkedn.setTag(""+speaker.getLinkedInURL());
		
		imgWeb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0)
			{
				if(arg0.getTag()!=null)
				{
					if(arg0.getTag().toString()!=null && !arg0.getTag().toString().equalsIgnoreCase(""))
						startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(arg0.getTag().toString())));
					else					
						utilities.showToast(Constants.DATA_NOT_AVAILABELE);
					
				}
				
			}
		});
		
		imgContact.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) 
					{
						if(arg0.getTag()!=null)
						{
							if(arg0.getTag().toString()!=null && !arg0.getTag().toString().equalsIgnoreCase(""))
							utilities.dialNumber(arg0.getTag().toString());
							else					
								utilities.showToast(Constants.DATA_NOT_AVAILABELE);
						}
						
					}
				});
		
		imgFacebook.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						if(arg0.getTag()!=null)
						{
							if(arg0.getTag().toString()!=null && !arg0.getTag().toString().equalsIgnoreCase(""))
								startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(arg0.getTag().toString())));
							else					
								utilities.showToast(Constants.DATA_NOT_AVAILABELE);
						}
						
					}
				});
		
		imgTwitter.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(arg0.getTag()!=null)
				{
					if(arg0.getTag().toString()!=null && !arg0.getTag().toString().equalsIgnoreCase(""))
						startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(arg0.getTag().toString())));
					else					
						utilities.showToast(Constants.DATA_NOT_AVAILABELE);
				}
				
			}
		});
		
		imgLinkedn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(arg0.getTag()!=null)
				{
					if(arg0.getTag().toString()!=null && !arg0.getTag().toString().equalsIgnoreCase(""))
					startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(arg0.getTag().toString())));
				}
				
			}
		});
		
		
imgPdf.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(arg0.getTag()!=null)
				{
					if(arg0.getTag().toString()!=null && !arg0.getTag().toString().equalsIgnoreCase(""))
					{
						Intent intentWebSite=new Intent(AboutSpeakers.this, WebActivity.class);	
						intentWebSite.putExtra("url", "http://docs.google.com/viewer?url="+arg0.getTag().toString());			
						startActivity(intentWebSite);
					}
				}
				
			}
		});
		
		
		
		//contact information and feedback
			
		imgEmail=(ImageView)findViewById(R.id.ximgEmail);
		imgRating=(ImageView)findViewById(R.id.ximgRating);
		ximgComment=(ImageView)findViewById(R.id.ximgComment);
		
		imgEmail.setOnClickListener(this);
		imgRating.setOnClickListener(this);
		ximgComment.setOnClickListener(this);
		
		

		if(speaker.getEmail()!=null)
		{
			if(!speaker.getEmail().equalsIgnoreCase("#")&& !speaker.getEmail().equalsIgnoreCase(""))
			{	
				imgEmail.setVisibility(View.VISIBLE);
				imgEmail.setTag(""+speaker.getEmail());				
			}
			else
			{
				imgEmail.setVisibility(View.GONE);
				
			}
		}
		
		utilities=Utilities.getInstance(activity);
		
		final Activity activity=this;
		ImageView imgBack=(ImageView)findViewById(R.id.xbtnVwBack);
		imgBack.setOnClickListener(new OnClickListener() 
		{
			
			@Override
			public void onClick(View arg0) 
			{
				finish();
				//startActivity(new Intent(activity,SpeakerActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				
			}
		});
		
	}

	@Override
	public void onProcesCompleted(int requestId, Object Data) 
	{	
		utilities.dismiss();
		if(((Responce)Data).getResponceData().toString().equals("Success"))
			utilities.showToast("Thank you for rating.");
	}

	@Override
	public void onErrorOccured(int requestId) 
	{
		utilities.dismiss();	
		utilities.showToast(Constants.ERROR_MESSAGE);
		
	}

	@Override
	public void onClick(View v) 
	{
		if(v==imgEmail)
		{
			utilities.sendEmail(speaker.getEmail(), "", "");
		}
		else if(v==imgRating)
		{
			utilities.showRatingDialog("Rate speaker","Please rate speaker",speaker.getID());
			utilities.setRatingDialogListener(new RatingDialogListener() 
			{
				
				@Override
				public void OnRatingSet(float rating, int id) 
				{
					WebServiceManager webServiceManager=new WebServiceManager(activity);
					webServiceManager.setonWebServiceListener(activity);
					webServiceManager.setSpeakerRate(id, rating);
					
				}
				
				@Override
				public void OnRatingCancelled()
				{
					// TODO Auto-generated method stub
					
				}
			});
		}
		else if(v==ximgComment)
		{
		
		}
		
	}
}
